package io.github.uicdb.badskill.clent.impl;

import io.github.uicdb.badskill.*;
import io.github.uicdb.badskill.clent.HorScrollTabLayout;
import io.github.uicdb.badskill.clent.HsTab;
import io.github.uicdb.badskill.clent.TextView;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.List;
import java.util.function.Supplier;

public class SkillScreen extends Screen {
    protected static Component UNSELECTED_TITLE = Component.translatable("tips.panel.unselected.title");
    protected static Component UNSELECTED_DESC = Component.translatable("tips.panel.unselected.desc");
    protected static Component BUTTON_LEARN = Component.translatable("panel.button.learn");
    protected static Component BUTTON_LEARNED = Component.translatable("panel.button.learned");
    protected static Component BUTTON_CANT_LEARN = Component.translatable("panel.button.cant_learn");
    public static final ResourceLocation BACKGROUND_LOCATION = new ResourceLocation(BadSkill.MODID, "textures/gui/panel_background.png");

    public SkillScreen() {
        super(Component.literal("skill screen"));
    }

    SkillPagePanel panel;
    int x, y, imageWidth, imageHeight;


    TextView titleView;
    VecScTextView descPanel;
    @Override
    protected void init() {
        super.init();
        assert minecraft != null;
        var width = Minecraft.getInstance().getWindow().getGuiScaledWidth();
        var height = Minecraft.getInstance().getWindow().getGuiScaledHeight();
        imageWidth = (int) (248 * getScale());
        imageHeight = (int) (166 * getScale());
        x = (width - imageWidth) / 2;
        y = (height - imageHeight) / 2;

        panel = new SkillPagePanel(px(5), py(29), (int) (181 * getScale()), (int) (132 * getScale()), 0.3, 2.7);
        HorScrollTabLayout layout = new HorScrollTabLayout(px(9), py(7), (int) (228 * getScale()), (int) (13 * getScale()), Component.literal("title"));
        for (int i = 0; i < SkillPageManager.INSTANCE.getPageList().size(); i++) {
            int finalI = i;
            layout.addTab(new HsTab(i, (int) (10 * getScale()), (int) (10 * getScale()), page -> {
                panel.reload(finalI);
            }, SkillPageManager.INSTANCE.safeGet(i).icon));
        }

        addRenderableWidget(layout);
        addRenderableWidget(titleView=new TextView(px(198), py(40), (int) (36 * getScale()), (int) (29*getScale()), () -> {
            var id = panel.selectedID;
            if (id != null) {
                var key = "tips.panel." + id.getNamespace() + "." + id.getPath() + ".title";
                if (I18n.exists(key))
                    return List.of(Component.translatable(key));
                else return List.of(Component.translatable("tips.panel.default.title", id.getPath()));
            } else {
                return List.of(UNSELECTED_TITLE);
            }
        }));
        titleView.setTextScale(0.8f);
        addRenderableWidget(descPanel= new VecScTextView(px(198), py(74), (int) (35 * getScale()), (int) (getScale() * 67), () -> {
            var id = panel.selectedID;
            if (id != null) {
                var option = SkillOptionManager.INSTANCE.OPTIONS.get(id);
                if (option != null) {
                    return option.getTooltips(Minecraft.getInstance().player);
                }
            }
            return List.of(UNSELECTED_DESC);
        }));
        descPanel.setTextScale(.7f);
        addRenderableWidget(new LearnButton(px(197), py(142), (int) (getScale() * 37), (int) (getScale() * 10), new Supplier<Component>() {
            @Override
            public Component get() {
                Component[] c = new Component[]{BUTTON_CANT_LEARN};
                var id = panel.selectedID;
                if (id != null) {
                    SkillOption option = SkillOptionManager.INSTANCE.OPTIONS.get(id);
                    if (option != null)
                        minecraft.player.getCapability(PlayerSkillCapabilityProvider.CAPABILITY)
                                .ifPresent(capability -> {
                                    var loc= panel.currentPage.locations.get(id);
                                    if (loc!=null) {
                                        PlayerSkillCapability.State state = capability.checkState(loc, option);
                                        if (state == PlayerSkillCapability.State.LEARNED) c[0] = BUTTON_LEARNED;
                                        if (state == PlayerSkillCapability.State.LEARNABLE) c[0] = BUTTON_LEARN;
                                    }
                                });
                    return c[0];
                } else
                    return UNSELECTED_TITLE;
            }
        }, pButton -> {
            var id = panel.selectedID;
            if (id != null ) {
                minecraft.player.getCapability(PlayerSkillCapabilityProvider.CAPABILITY)
                        .ifPresent(capability -> {
                            if (capability.learn(panel.currentPage.id, id, true))
                                capability.learn(panel.currentPage.id, id, false);
                        });
            }
        }));
        addRenderableWidget(panel);
    }

    private int px(int dx) {
        return (int) (x + dx * getScale());
    }

    private int py(int dy) {
        return (int) (y + dy * getScale());
    }

    private double getScale() {
        return 1.4;
    }

    @Override
    public void render(GuiGraphics pPoseStack, int pMouseX, int pMouseY, float pPartialTick) {
        renderBackground(pPoseStack);
        super.render(pPoseStack, pMouseX, pMouseY, pPartialTick);
    }


    @Override
    public void renderBackground(GuiGraphics pPoseStack) {
        pPoseStack.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);

        pPoseStack.blit(BACKGROUND_LOCATION, px(0), py(0), 0, 0, imageWidth, imageHeight, imageWidth, imageHeight);
    }
}
